home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / mit / bsd / host.c next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  1.4 KB  |  88 lines

  1.  
  2. /*
  3.  *    $Header: host.c,v 3.0 91/05/17 16:14:06 jrd Rel $
  4.  *    Author: J. Davin
  5.  *    Copyright 1988, 1989, Massachusetts Institute of Technology
  6.  *    See permission and disclaimer notice in file "notice.h"
  7.  */
  8.  
  9. #include    <notice.h>
  10.  
  11. #include    <sys/types.h>
  12. #include    <sys/socket.h>
  13. #include    <netinet/in.h>
  14. #include    <arpa/inet.h>
  15. #include    <netdb.h>
  16. #include    <stdio.h>
  17.  
  18. #include    <local.h>
  19. #include    <host.h>
  20.  
  21. long        hostAddress (string)
  22.  
  23. char        *string;
  24.  
  25. {
  26.     struct        hostent        *hp;
  27.     long        host;
  28.  
  29.     host = (long) inet_addr (string);
  30.     if (host == -1L) {
  31.         hp = gethostbyname (string);
  32.         if (hp == NULL) {
  33.             return (-1);
  34.         }
  35.         else if (hp->h_addrtype != AF_INET) {
  36.             return (-1);
  37.         }
  38.         else {
  39.             host = 0L;
  40.             bcopy (hp->h_addr, (char *) & host,
  41.                 sizeof (host));
  42.         }
  43.     }
  44.     return (host);
  45. }
  46.  
  47. int        hostString (result, n, host)
  48.  
  49. char        *result;
  50. int        n;
  51. long        host;
  52.  
  53. {
  54.     struct        hostent        *hp;
  55.     struct        in_addr        in;
  56.     int                k;
  57.     char                *cp;
  58.  
  59.     hp = gethostbyaddr ((char *) & host, (int) sizeof (host),
  60.         (int) AF_INET);
  61.     if (hp != NULL) {
  62.         k = strlen (hp->h_name);
  63.         if (k > n) {
  64.             return (0);
  65.         }
  66.         else {
  67.             (void) strcpy (result, hp->h_name);
  68.             return (k);
  69.         }
  70.     }
  71.     else {
  72.         (void) bzero ((char *) & in, (int) sizeof (in));
  73.         in.s_addr = (u_long) host;
  74.         cp = inet_ntoa (in);
  75.         if (cp == (char *) NULL) {
  76.             return (0);
  77.         }
  78.         else if ((k = strlen (cp)) > n) {
  79.             return (0);
  80.         }
  81.         else {
  82.             (void) strcpy (result, cp);
  83.             return (k);
  84.         }
  85.     }
  86. }
  87.  
  88.